<include file="Public/head"/>
<body class="no-skin">
<include file="Public/header"/>

<div class="main-container" id="main-container">


    <include file="Public/sidebar"/>
    <div class="main-content">
        <div class="main-content-inner">
            <!-- #section:basics/content.breadcrumbs -->
            <include file="Public/breadcrumbs"/>

            <!-- /section:basics/content.breadcrumbs -->
            <div class="page-content">
                <include file="Public/set"/>

                <!-- /section:settings.box -->
                <div class="row">
                    <div class="col-xs-12">
                        <form class="form-horizontal" id="form" method="post" action="{:U('update')}">

                            <!-- PAGE CONTENT BEGINS -->

                            <input type="hidden" name="id" value="{$res.id}" id="id"/>

                            <div class="space-4"></div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    凭证编号 </label>
                                <div class="col-sm-2">
                                    <span class="col-sm-1 grain-line-height" style="color: red">{$res.voucher_no}</span>
                                    <input type="hidden" name="shuifen"
                                           class="col-xs-10 col-sm-4" value="{$res['voucher_no']}">
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" >
                                    场地粮区 </label>
                                <div class="col-sm-2">

                                    <select id="warehouse_id" name="warehouse_id" class="col-xs-10 col-sm-12">
                                        <option value="">--- 请选择仓库 ---</option>
                                        <volist name="warehouse_array" id="vo">
                                            <eq name="res.warehouse_id" value="$vo.id">
                                                <option value="{$vo.id}" selected="selected">{$vo.name}</option>
                                                <else/>
                                                <option value="{$vo.id}">{$vo.name}</option>
                                            </eq>

                                        </volist>
                                    </select>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" >
                                    日期
                                </label>
                                <div class="col-sm-4">
                                    <div class="input-group col-xs-5">
                                        <eq name="res.date" value="">
                                            <input class="form-control"  name="date" value="{$date|date='Y-m-d',###}" type="text" data-date-format="dd-mm-yyyy" />
                                            <else/>
                                            <input class="form-control" name="date" value="{$res['date']|date='Y-m-d',###}" type="text" data-date-format="dd-mm-yyyy" />
                                        </eq>
                                        <span class="input-group-addon">
                                            <i class="ace-icon fa fa-calendar bigger-110"></i>
                                        </span>
                                    </div>
                                    <span class="help-inline col-xs-12 col-sm-7">
                                        <span class="middle"></span>
                                    </span>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" >
                                    姓名 </label>
                                <div class="col-sm-2">
                                    <input type="text" name="name" id="name" placeholder="请填写售粮人姓名" class="col-xs-10 col-sm-12" value="{$res['name']}">
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" >
                                    身份证号 </label>
                                <div class="col-sm-2">
                                        <input type="text" name="ID_number" id="ID_number" placeholder="请填写售粮人身份证号" class="col-xs-10 col-sm-12" value="{$res['ID_number']}">
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" >
                                    银行卡 </label>
                                <div class="col-sm-2">
                                    <input type="text" name="card_number"  placeholder="请填写售粮人银行卡号" class="col-xs-10 col-sm-12" value="{$res['card_number']}">
                                </div>
                            </div>
                            <!--<div class="space-4"></div>-->
                            <!--<div class="form-group">-->
                                <!--<label class="col-sm-1 control-label no-padding-right" >-->
                                    <!--地址 </label>-->
                                <!--<div class="col-sm-2">-->
                                    <!--<input type="text" name="address"  placeholder="请填写售粮人地址" class="col-xs-10 col-sm-12" value="{$res['card_number']}">-->
                                <!--</div>-->
                            <!--</div>-->
                            <div class="space-4"></div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" >
                                    粮食品种 </label>
                                <div class="col-sm-2">
                                        <select id="foodstuff_id" name="foodstuff_id" class="col-xs-10 col-sm-12">
                                            <option value="">--- 请选择粮食品种 ---</option>
                                            <volist name="foodstuff_array" id="vo">
                                                <eq name="res.foodstuff_id" value="$vo.id">

                                                    <option value="{$vo.id}" selected="selected">{$vo.name}</option>

                                                    <else/>

                                                    <option value="{$vo.id}">{$vo.name}</option>
                                                </eq>

                                            </volist>
                                        </select>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" >
                                    等级 </label>
                                <div class="col-sm-2">

                                    <select id="level"  name="level" class="col-xs-10 col-sm-12">
                                        <option value="">--- 请选择粮食等级 ---</option>
                                        <for start="1" end="6">
                                            <eq name="res.level" value="$i">
                                                <option value="{$i}" selected="selected">{$i}</option>
                                                <else/>
                                                <option value="{$i}">{$i}</option>
                                            </eq>
                                        </for>
                                    </select>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    单价
                                </label>
                                <div class="col-sm-2">
                                    <input type="text" name="price" id="price"
                                           class="col-xs-10 col-sm-7 data" style="color: red"  value="{$res['price']}">
                                    <span class="col-sm-5 grain-line-height">&nbsp;元 / kg</span>

                                </div>
                            </div>


                            <div class="space-4"></div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right">
                                    水分 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="shuifen" id="shuifen"
                                           class="col-xs-10 col-sm-8 data" value="{$res['shuifen']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;%</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    生霉率 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="shengmei" id="shengmei"
                                           class="col-xs-10 col-sm-8 data" value="{$res['shengmei']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;%</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    杂质 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="zazhi" id="zazhi"
                                           class="col-xs-10 col-sm-8 data" value="{$res['zazhi']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;%</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    出糙率 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="chucaolv" id="chucaolv"
                                           class="col-xs-10 col-sm-8 data" value="{$res['chucaolv']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;%</span>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    整精米率 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="zhengjingmilv" id="zhengjingmilv"
                                           class="col-xs-10 col-sm-8 data" value="{$res['zhengjingmilv']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;%</span>
                                </div>

                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    谷外糙米率 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="guwaicaomilv" id="guwaicaomilv"
                                           class="col-xs-10 col-sm-8 data" value="{$res['guwaicaomilv']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;%</span>

                                </div>

                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    黄粒米 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="huanglimi" id="huanglimi"
                                           class="col-xs-10 col-sm-8 data" value="{$res['huanglimi']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;%</span>
                                </div>

                            </div>
                            <div class="space-4"></div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    车型
                                </label>

                                <div class="col-sm-2">
                                    <select id="cartype_id" name="cartype_id" class="col-xs-10 col-sm-12">
                                        <option value="">--- 请选择车型 ---</option>
                                        <volist name="cartype_array" id="vo">
                                            <eq name="res.cartype_id" value="$vo.id">
                                                <option value="{$vo.id}" selected="selected">{$vo.name}</option>
                                                <else/>
                                                <option value="{$vo.id}">{$vo.name}</option>
                                            </eq>
                                        </volist>
                                    </select>
                                </div>


                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">
                                    车牌照号
                                </label>
                                <div class="col-sm-2">
                                    <input type="text" name="price" id="car" placeholder="请填写车牌信息" class="col-xs-10 col-sm-12" value="{$res['price']}">
                                </div>
                            </div>

                            <div class="space-4"></div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right">毛重 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="maozhong" id="maozhong"
                                           class="col-xs-10 col-sm-8 weight" value="{$res['maozhong']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;kg</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right">皮重 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="pizhong" id="pizhong"
                                           class="col-xs-10 col-sm-8 weight" value="{$res['pizhong']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;kg</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right">场地扣量 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="changdikouliang" id="changdikouliang"
                                           class="col-xs-10 col-sm-8 weight" value="{$res['changdikouliang']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;kg</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right"> 净重 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="jingzhong" id="jingzhong" style="color:red"
                                           class="col-xs-10 col-sm-8 grain-important-info weight" value="{$res['jingzhong']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;kg</span>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right">粮食价款 </label>
                                <div class="col-sm-2">
                                    <span class="col-sm-4 grain-line-height" id="liangshijiakuan-text" style="color:red; ">0.00</span>
                                    <input type="hidden" name="liangshijiakuan" id="liangshijiakuan"
                                           class="col-xs-10 col-sm-8 grain-important-info" value="{$res['liangshijiakuan']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;元</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right">扣装卸费 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="kouzhuangxiefei" id="kouzhuangxiefei"
                                           class="col-xs-10 col-sm-8" value="{$res['kouzhuangxiefei']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;元</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right">结算价款 </label>
                                <div class="col-sm-2">
                                    <span class="col-sm-4 grain-line-height " id="jiesuanjiakuan-text" style="color:red; ">0.00</span>
                                    <input type="hidden" name="jiesuanjiakuan" id="jiesuanjiakuan"
                                           class="col-xs-10 col-sm-8" value="{$res['jiesuanjiakuan']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;元</span>
                                </div>
                                <label class="col-sm-1 control-label no-padding-right">实际价款 </label>
                                <div class="col-sm-1">
                                    <input type="text" name="shijijiakuan" id="shijijiakuan" style="color:red"
                                           class="col-xs-10 col-sm-8" value="{$res['jiesuanjiakuan']}">
                                    <span class="col-sm-1 grain-line-height">&nbsp;元</span>
                                </div>
                            </div>

                            <div class="col-md-offset-2 col-md-9">
                                <button class="btn btn-info submit" type="button">
                                    <i class="icon-ok bigger-110"></i>
                                    提交
                                </button>
                                &nbsp; &nbsp; &nbsp;
                                <button class="btn" type="reset">
                                    <i class="icon-undo bigger-110"></i>
                                    重置
                                </button>
                            </div>

                        </form>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->
    <include file="Public/footer"/>

</div><!-- /.main-container -->

<include file="Public/footerjs"/>
<!-- inline scripts related to this page -->
<script src="__PUBLIC__/qwadmin/js/date-time/bootstrap-datepicker.js"></script>
<!--<script src="__PUBLIC__/qwadmin/js/function.js"></script>-->
<!--parameter--------------------------------->
<input id="isParameter" class="data" type="hidden" >
<input id="shuifen-parameter" class="data" type="hidden">
<input id="shengmei-parameter" class="data" type="hidden">
<input id="zazhi-parameter" class="data" type="hidden">
<input id="chucaolv-parameter" class="data" type="hidden">
<input id="zhengjingmilv-parameter" class="data" type="hidden">
<input id="guwaicaomilv-parameter" class="data" type="hidden">
<input id="huanglimi-parameter" class="data" type="hidden">
<input id="price-parameter" class="data" type="hidden">
<!--setting-------------------------------->
<!--水分-->
<input id="shuifen-number" class="data" type="hidden">
<input id="shuifen-minus" class="data" type="hidden">
<input id="shuifen-plus" class="data" type="hidden">
<input id="shuifen-shangliang" class="data" type="hidden">
<!--生霉-->
<input id="shengmei-number" class="data" type="hidden">
<input id="shengmei-minus" class="data" type="hidden">
<input id="shengmei-plus" class="data" type="hidden">
<!--杂质-->
<input id="zazhi-number" class="data" type="hidden">
<input id="zazhi-minus" class="data" type="hidden">
<input id="zazhi-plus" class="data" type="hidden">
<input id="zazhi-qingzafei" class="data" type="hidden">
<!--出糙率-->
<input id="chucaolv-number" class="data" type="hidden">
<input id="chucaolv-minus" class="data" type="hidden">
<input id="chucaolv-plus" class="data" type="hidden">
<!--整精米率-->
<input id="zhengjingmilv-number" class="data" type="hidden">
<input id="zhengjingmilv-minus" class="data" type="hidden">
<input id="zhengjingmilv-plus" class="data" type="hidden">
<!--谷外糙米率-->
<input id="guwaicaomilv-number" class="data" type="hidden">
<input id="guwaicaomilv-minus" class="data" type="hidden">
<input id="guwaicaomilv-plus" class="data" type="hidden">
<!--黄粒米-->
<input id="huanglimi-number" class="data" type="hidden">
<input id="huanglimi-minus" class="data" type="hidden">
<input id="huanglimi-plus" class="data" type="hidden">

<script type="text/javascript">

    //结算计算器
    function finalCalculator(){
        var liangshijiakuan = isnan(parseFloat($("#liangshijiakuan").val()));
        var kouzhuangxiefei = isnan(parseFloat($("#kouzhuangxiefei").val()));
        var final = liangshijiakuan-kouzhuangxiefei;
        return final.toFixed(2);

    }

    //价款计算器
    function totalCalculator(){
        var price= isnan(parseFloat($("#price").val()));
        var jingzhong = isnan(parseFloat($("#jingzhong").val()));
        var total = 0;
        if(price!=0 && jingzhong!=0){
            total = price*jingzhong;
        }
        return total.toFixed(2);
    }

    //重量计算器
    function weightCalculator(){
        var maozhong = isnan(parseFloat($("#maozhong").val()));
        var pizhong = isnan(parseFloat($("#pizhong").val()));
        var changdikouliang = isnan(parseFloat($("#changdikouliang").val()));
        var jingzhong =0 ;
        if(maozhong!=0 && pizhong!=0 && changdikouliang!=0 && maozhong>pizhong+changdikouliang){
            jingzhong = maozhong-pizhong-changdikouliang;
        }
        return jingzhong.toFixed(2);


    }

    //价格计算器
    function priceCalculator(){
        var isParameter = $("#isParameter").val();
        if(isParameter!=1){
            return 0;
        }
        var price = isnan(parseFloat($("#price-parameter").val()));
//        price = price * priceFormula("shuifen");
        price = price * priceFormula("shuifen")*
        priceFormula("shengmei")*
        priceFormula("zazhi")*
        priceFormula("chucaolv")*
        priceFormula("zhengjingmilv",1)*
        priceFormula("guwaicaomilv")*
        priceFormula("huanglimi");
        price = isnan(price);
        return price.toFixed(2);
    }

    //单价计算公式
    function priceFormula(name,direction){

        var value =  isnan(parseFloat($("#"+name).val()));
        var parameter =  isnan(parseFloat($("#"+name+"-parameter").val()));
        var number =  isnan(parseFloat($("#"+name+"-number").val()));
        var minus =  isnan(parseFloat($("#"+name+'-minus').val()));
        var plus =  isnan(parseFloat($("#"+name+"-plus").val()));

        //direction为0时,实际值大于标准值产生扣量,为1时,实际值大于标准值产生增量
        direction = isNaN(direction) ? 0 : direction;
        var diff =  parameter-value;
        diff = Math.abs(diff);
        var res;


        if(number<=0){
            return 1
        }
        if(parameter>=value){
            if(direction==1){
                res = 1-(diff/number*minus);
            }else{
                res = 1+(diff/number*plus);
            }

        }else{

            if(name=="shuifen"){
                minus += isnan(parseFloat($("#"+name+"-shangliang").val()));
            }
            if(name == "qingzafei"){
                minus += isnan(parseFloat($("#"+name+"-qingzafei").val()));
            }
            if(direction==1){
                res = 1+(diff/number*plus);
            }else{
                res = 1-(diff/number*minus);
            }
        }
        return res;
    }

    function isnan(value){
        return isNaN(value) ? 0 : value;

    }

    //获取品类等级参数
    function getParameter(foodstuff_id,level){
            $.ajax({
                type:'post',
                dataType:"json",
                url:"{:U('getParameter')}",
                data:{foodstuff_id:foodstuff_id,level:level},
                success:function(msg){
                    $("#isParameter").val(msg.status);
                    if(msg.status==1){
                        //显示参数
                        showParameter('shuifen',msg.parameter_res.shuifen);
                        showParameter('shengmei',msg.parameter_res.shengmei);
                        showParameter('zazhi',msg.parameter_res.zazhi);
                        showParameter('chucaolv',msg.parameter_res.chucaolv);
                        showParameter('zhengjingmilv',msg.parameter_res.zhengjingmilv);
                        showParameter('guwaicaomilv',msg.parameter_res.guwaicaomilv);
                        showParameter('huanglimi',msg.parameter_res.huanglimi);
                        showParameter('price',msg.parameter_res.price);
                        //存储参数
                        saveParameter('shuifen',msg.parameter_res.shuifen);
                        saveParameter('shengmei',msg.parameter_res.shengmei);
                        saveParameter('zazhi',msg.parameter_res.zazhi);
                        saveParameter('chucaolv',msg.parameter_res.chucaolv);
                        saveParameter('zhengjingmilv',msg.parameter_res.zhengjingmilv);
                        saveParameter('guwaicaomilv',msg.parameter_res.guwaicaomilv);
                        saveParameter('huanglimi',msg.parameter_res.huanglimi);
                        saveParameter('price',msg.parameter_res.price);
                        //存储水分设置
                        saveSetting('shuifen',msg.shuifen_res.number,msg.shuifen_res.minus,msg.shuifen_res.plus,'shangliang',msg.shuifen_res.shangliang);
                        //存储生霉设置
                        saveSetting('shengmei',msg.shengmei_res.number,msg.shengmei_res.minus,msg.shengmei_res.plus);
                        //存储杂质设置
                        saveSetting('zazhi',msg.zazhi_res.number,msg.zazhi_res.minus,msg.zazhi_res.plus,'qingzafei',msg.zazhi_res.qingzafei);
                        //存储出糙率设置
                        saveSetting('chucaolv',msg.chucaolv_res.number,msg.chucaolv_res.minus,msg.chucaolv_res.plus);
                        //存储整精米率设置
                        saveSetting('zhengjingmilv',msg.zhengjingmilv_res.number,msg.zhengjingmilv_res.minus,msg.zhengjingmilv_res.plus);
                        //存储谷外糙米率设置
                        saveSetting('guwaicaomilv',msg.guwaicaomilv_res.number,msg.guwaicaomilv_res.minus,msg.guwaicaomilv_res.plus);
                        //存储黄粒米设置
                        saveSetting('huanglimi',msg.huanglimi_res.number,msg.huanglimi_res.minus,msg.huanglimi_res.plus);
                    }else if(msg.status==2){
                        $(".data").val(0);
                    }


                }
            })
    }

    //显示参数到页面
    function showParameter(name,value){
        $("#"+name).val(value);
    }
    //将参数存储到页面
    function saveParameter(name,value){
        $("#"+name+"-parameter").val(value);
    }

    //将设置存储到页面
    function saveSetting(name,number,minus,plus,other,otherval){
        $("#"+name+"-number").val(number);
        $("#"+name+"-minus").val(minus);
        $("#"+name+"-plus").val(plus);
        if(other!="" && otherval!=""){
            $("#"+name+"-"+other).val(otherval);
        }
    }

    $(function () {
        $(".data").val(0);

        $("#foodstuff_id").change(function(){
            var foodstuff_id = $("#foodstuff_id").val();
            var level = $("#level").val();
            if(foodstuff_id!="" && level!=""){
                getParameter(foodstuff_id,level);
            }
        });

        $("#level").change(function(){
            var foodstuff_id = $("#foodstuff_id").val();
            var level = $("#level").val();
            if(foodstuff_id!="" && level!=""){
                getParameter(foodstuff_id,level);
            }
        });


        $(".data").not("[name='price']").keyup(function(){
            var price = priceCalculator();
            $("#price").val(price);

            var total = totalCalculator();
            $("#liangshijiakuan").val(total);
            $("#liangshijiakuan-text").html(total);

            var final = finalCalculator();
            $("#jiesuanjiakuan").val(final);
            $("#jiesuanjiakuan-text").html(final);
            $("#shijijiakuan").val(final);


        });

        $(".weight").not("[name='jingzhong']").keyup(function(){
            var jingzhong = weightCalculator();
            $("#jingzhong").val(jingzhong);

            var total = totalCalculator();
            $("#liangshijiakuan").val(total);
            $("#liangshijiakuan-text").html(total);

            var final = finalCalculator();
            $("#jiesuanjiakuan").val(final);
            $("#jiesuanjiakuan-text").html(final);
            $("#shijijiakuan").val(final);
        });

        $("#kouzhuangxiefei").keyup(function(){
            var final = finalCalculator();
            $("#jiesuanjiakuan").val(final);
            $("#jiesuanjiakuan-text").html(final);
            $("#shijijiakuan").val(final);

        });


        jQuery(function ($) {
            $('[name="date"]').datepicker({
                format: 'yyyy-mm-dd',
                weekStart: 1,
                autoclose: true,
                todayBtn: 'linked',
                language: 'cn'
            });
        });

        $(".submit").click(function () {
//            var shuifen = $("#shuifen").val();
//            if (shuifen == '') {
//                bootbox.dialog({
//                    title: '友情提示：',
//                    message: "仓库名称必须填写。",
//                    buttons: {
//                        "success": {
//                            "label": "确定",
//                            "className": "btn-danger"
//                        }
//                    }
//                });
//                return;
//            }
            $("#form").submit();
        });
    });
</script>
</body>
</html>
